package com.epfresh.supplier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;

import com.epfresh.api.entity.RequestEntityMap;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.OrderTrackSimpleViewBinder;
import com.epfresh.supplier.adapter.OrderTrackViewBinder;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.OrderTrackResponse;
import com.epfresh.supplier.entity.OrderTrackWord;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import multitype.MultiTypeAdapter;

public class OrderTrackActivity extends BaseActivity {
    String grouponId;

    RecyclerView mRv;
    MultiTypeAdapter mMultiAdapter;
    LinearLayoutManager mManager;
    List<Object> mItems = new ArrayList<>();

    private void initView() {

        setToolbarTitle("订货跟踪");


        mRv = (RecyclerView) findViewById(R.id.rv);
        mManager = new LinearLayoutManager(this);
        mRv.setLayoutManager(mManager);

        OrderTrackSimpleViewBinder orderTrackSimpleViewBinder = new OrderTrackSimpleViewBinder();
        OrderTrackViewBinder orderTrackViewBinder = new OrderTrackViewBinder();
        mMultiAdapter = new MultiTypeAdapter();

        mMultiAdapter.register(Pair.class, orderTrackSimpleViewBinder);
        mMultiAdapter.register(OrderTrackWord.class, orderTrackViewBinder);
        mMultiAdapter.setItems(mItems);
        mRv.setAdapter(mMultiAdapter);


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            getIntent().putExtra(Constant.TYPE, savedInstanceState.getInt(Constant.TYPE));
            getIntent().putExtra("grouponId", savedInstanceState.getString("grouponId"));
        }

        grouponId = getIntent().getStringExtra("grouponId");
        setContentView(R.layout.activity_order_track);
        initView();
        reqOrderTrackInfo();
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("grouponId", getIntent().getStringExtra("grouponId"));
        super.onSaveInstanceState(outState);
    }


    @Override
    public void onBackPressed() {


        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    void updateView(OrderTrackResponse otr) {

        if (otr != null) {
            List<Pair<String, String>> baseInfo = otr.getBaseInfo();
            mItems.clear();
            if (baseInfo != null && baseInfo.size() > 0) {
                mItems.addAll(baseInfo);
            }
            List<OrderTrackWord> orderTrackList = otr.getOrderTrackList();
            if (orderTrackList != null && orderTrackList.size() > 0) {
                mItems.addAll(orderTrackList);
            }

            mMultiAdapter.notifyDataSetChanged();
        }
    }


    private void reqOrderTrackInfo() {


        RequestEntityMap data = new RequestEntityMap();
        data.setCmd("pm-order/baseorder/groupon/node");
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());

        data.putParameter("buyerStoreId", BaseApplication.getInstance().getStoreId());
        data.putParameter("grouponId", grouponId);


        request(data, "pm-order/baseorder/groupon/node", new OnRequestListener<OrderTrackResponse>() {
            @Override
            public OrderTrackResponse jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, OrderTrackResponse.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
            }

            @Override
            public void onStart(Object tag) {

                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<OrderTrackResponse> response, Object tag) {
                hideProgressDialog();

                if (response != null) {
                    updateView(response.getResponseElement());
                }
            }
        });
    }


}
